namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System.Xml.Serialization;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using Xunit;

    public sealed class CheckBoxInputFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<CheckBoxInput>()
                .DerivesFrom<Input>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IXmlSerializable>()
                .XmlRoot("input")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            CheckBoxInput expected = new CheckBoxInput(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                AlternativeText = "alternative",
                Checked = true,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Size = 50,
                TabIndex = 1,
                Value = "value"
            };

            string xml = "<input id='id' class='class' lang='fr' name='id' accesskey='a' alt='alternative' tabindex='1' disabled='disabled' readonly='readonly' size='50' type='checkbox' value='value' checked='checked' />";
            CheckBoxInput actual = xml.Deserialize<CheckBoxInput>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<input type='checkbox' />".Deserialize<CheckBoxInput>());
        }

        [Fact]
        public void serialize()
        {
            CheckBoxInput obj = new CheckBoxInput(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                AlternativeText = "alternative",
                Checked = true,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Size = 50,
                TabIndex = 1,
                Value = "value"
            };

            string xpath = "1=count(/input[@id='id'][@class='class'][@lang='fr'][@name='id'][@tabindex='1'][@disabled='disabled'][@accesskey='a'][@alt='alternative'][@readonly='readonly'][@size='50'][@type='checkbox'][@value='value'][@checked='checked'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new CheckBoxInput());
        }

        [Fact]
        public void ctor_Name()
        {
            Assert.NotNull(new CheckBoxInput(new Name("id")));
        }

        [Fact]
        public void ctor_NameNull()
        {
            Assert.NotNull(new CheckBoxInput(null));
        }

        [Fact]
        public void prop_Checked()
        {
            Assert.True(new PropertyExpectations(typeof(CheckBoxInput).GetProperty("Checked"))
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Type()
        {
            Assert.True(new PropertyExpectations(typeof(CheckBoxInput).GetProperty("Type"))
                .DefaultValueIs("checkbox")
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            CheckBoxInput expected = new CheckBoxInput(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                AlternativeText = "alternative",
                Checked = true,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Size = 50,
                TabIndex = 1,
                Value = "value"
            };

            string xml = "<input id='id' class='class' lang='fr' name='id' accesskey='a' alt='alternative' tabindex='1' disabled='disabled' readonly='readonly' size='50' type='checkbox' value='value' checked='checked' />";
            CheckBoxInput actual = new CheckBoxInput().Deserialize("input", "checkbox", xml) as CheckBoxInput;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void op_ToDataCollection()
        {
            CheckBoxInput obj = "<input name='test' type='checkbox' value='123' checked='checked' />".Deserialize<CheckBoxInput>();

            DataCollection expected = new DataCollection();
            expected.Add("test", "123");

            DataCollection actual = obj.ToDataCollection();

            Assert.Equal<DataCollection>(expected, actual);
        }

        [Fact]
        public void op_Update_string_string()
        {
            Name name = "test";

            CheckBoxInput obj = new CheckBoxInput(name);

            obj.Update(name, string.Empty);

            Assert.True(obj.Checked);
        }

        [Fact]
        public void op_Update_string_stringEmpty()
        {
            Name name = "test";

            CheckBoxInput obj = new CheckBoxInput(name);

            obj.Update(name, string.Empty);

            Assert.True(obj.Checked);
        }

        [Fact]
        public void op_Update_string_stringNull()
        {
            Name name = "test";

            CheckBoxInput obj = new CheckBoxInput(name);

            obj.Update(name, null);

            Assert.True(obj.Checked);
        }

        [Fact]
        public void op_Update_stringNotName_string()
        {
            CheckBoxInput obj = new CheckBoxInput("foo");

            obj.Update("bar", string.Empty);

            Assert.False(obj.Checked);
        }
    }
}